<template>
  <div class="login">
    <el-form ref="myform" :rules="rules" class="myform" :model="form">
      <h3>iHRM 后台登录系统</h3>
      <el-form-item prop="mobile">
        <el-input v-model="form.mobile" placeholder="请输入手机号" prefix-icon="el-icon-user-solid" />
      </el-form-item>
      <el-form-item prop="password">
        <el-input v-model="form.password" placeholder="请输入密码" prefix-icon="el-icon-lock" />
      </el-form-item>
      <el-form-item>
        <el-button class="btn" type="primary" @click="login">登录</el-button>
      </el-form-item>
      <el-form-item>
        <div class="zhuce">还没有账号? 立即注册</div>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
// import { loginApi } from '@/api/user.js'
import { mapActions } from 'vuex'
export default {
  data() {
    return {
      // 表单数据
      form: {
        mobile: '13800000002',
        password: '123456'
      },
      // 验证规则
      rules: {
        mobile: [
          { required: true, message: '手机号不能为空', trigger: 'blur' },
          {
            validator: (rule, value, callback) => {
              const reg = /^(0|86|17951)?(13[0-9]|15[012356789]|166|17[3678]|18[0-9]|14[57])[0-9]{8}$/
              reg.test(value) ? callback() : callback(new Error('手机号不合法'))
            }
          }
        ],
        password: [
          { required: true, message: '密码不能为空', trigger: 'blur' },
          { min: 3, max: 6, message: '密码长度为 3-6 位', trigger: 'blur' }
        ]
      }
      // rules: {
      //   mobile: [
      //     { required: true, message: '手机号不能为空!', trigger: 'blur' },
      //     {
      //       validator: (rule, value, callback) => {
      //         const reg = /^(0|86|17951)?(13[0-9]|15[012356789]|166|17[3678]|18[0-9]|14[57])[0-9]{8}$/
      //         reg.test(value) ? callback() : callback(new Error('请输入合法的手机号!'))
      //       }
      //     }
      //     // {
      //     // // 只能匹配正则
      //     //   pattern: /^(0|86|17951)?(13[0-9]|15[012356789]|166|17[3678]|18[0-9]|14[57])[0-9]{8}$/,
      //     //   message: '请输入正确的手机号',
      //     //   trigger: 'blur'
      //     // }
      //   ],
      //   password: [
      //     { required: true, message: '密码不能为空!', trigger: 'blur' },
      //     { min: 3, max: 6, message: '密码长度为3~6位', trigger: 'blur' }
      //   ]
      // }
    }
  },
  methods: {
    ...mapActions({
      vuexLogin: 'user/login'
    }),
    login() {
      // 全局表单验证
      this.$refs.myform.validate(async result => {
        if (result) {
          // 验证通过
          // 发送请求
          // const res = await loginApi(this.form)
          // 将 token 保存到 localstorage 中, 跳转到页面
          // console.log(res)
          await this.vuexLogin(this.form)
          this.$router.push('/')
        } else {
          // 验证失败
          console.log('验证失败')
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.login {
  background-image: url('../../assets/common/login1.png');
  height: 100%;
  background-position: center;
  .myform {
    width: 520px;
    height: 400px;
    padding: 36px;
    padding-top: 0px;
    box-sizing: border-box;
    position: fixed;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -70%);
    h3 {
      font-size: 48px;
      font-weight: 200;
      letter-spacing: 2px; // 设置文本之间的间隔
      color: #fff;
      line-height: 1;
    }
    ::v-deep input.el-input__inner {
      height: 55px;
      background-color: rgba(255, 255, 255, 0.8);
      border: none;
      // color: #68b0fe;
      color: #000;
      border-radius: 6px;
      padding-left: 40px;
    }
    .btn {
      width: 100%;
      height: 55px;
      font-size: 24px;
      background-color: #407ffe;
      border-radius: 6px;
      border-style: none;
      color: #fff;
    }
    .zhuce {
      font-size: 18px;
      text-align: center;
      color: #fff;
    }
    ::v-deep .el-form-item__error {
      color: #fff;
    }
  }
}
</style>
